package com.modules.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by lewis on 2017/9/9.
 */
public abstract class BeanUtils extends  org.springframework.beans.BeanUtils{
	
	public static final String X_REQUESTED_WIDTH="X-Requested-With";
    public static final String XML_HTTP_REQUEST="XMLHttpRequest";
    /**
     * 拷贝指定的属性
     * gyzhang@20131207@nanhai
     * @param source
     * @param target
     * @param assignProperties 指定的属性
     * @throws BeansException
     */
    public static void copyAssignProperties(Object source, Object target, String[] assignProperties)
            throws BeansException {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");

        Class<?> actualEditable = target.getClass();

        PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
        List<String> assignList = (assignProperties != null) ? Arrays.asList(assignProperties) : null;

        for (PropertyDescriptor targetPd : targetPds) {
            if (targetPd.getWriteMethod() != null &&
                    (assignProperties == null || (assignList.contains(targetPd.getName())))) {
                PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
                if (sourcePd != null && sourcePd.getReadMethod() != null) {
                    try {
                        Method readMethod = sourcePd.getReadMethod();
                        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                            readMethod.setAccessible(true);
                        }
                        Object value = readMethod.invoke(source);
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, value);
                    }
                    catch (Throwable ex) {
                        throw new FatalBeanException("Could not copy properties from source to target", ex);
                    }
                }
            }
        }
    }
    
    
    /**
     * 判断是否ajax请求.
     * 可以看到Ajax 请求多了个 x-requested-with ，可以利用它，
     * request.getHeader("x-requested-with"); 为 null，则为传统同步请求，为 XMLHttpRequest，则为Ajax 异步请求。
     *@paramrequest  HttpServletRequest
     *@return是否ajax请求.
     */
   public static boolean isAjaxRequest(HttpServletRequest request) {
        String xr=request.getHeader(X_REQUESTED_WIDTH);
       return(xr!=null&&XML_HTTP_REQUEST.equalsIgnoreCase(xr));
    }

   public static String getCookieValue(HttpServletRequest request, String name) {
       Cookie[]cookies=request.getCookies();
      if(cookies!=null) {
          for(Cookie cookie:cookies) {
               String cookieName=cookie.getName();
              if(cookieName.equals(name)) {
                  return cookie.getValue();
               }
           }
       }
      return null;
   }
}
    


